﻿/*<summary>
Created By : Swati
Created On : 10/02/2008
Class Name : PatientRegistration_DataLoader
Used In    : 
------------------------------------------------------------------------
Modification Log  
------------------------------------------------------------------------
Created By        Modified By         Remarks                             
-----------       ------------        ----------------------------------
                                                                          
------------------------------------------------------------------------
</summary>*/

using System.Xml;
using System.Data;
using System;
using Utils;
using System.Data.SqlClient;

namespace RegistrationController
{
    public class PatientRegistration_DataLoader
    {
        public PatientInfo Get_PATIENT_DATA(string PID)
        {
            PatientInfo objPatient = new PatientInfo();
            ezUtilsAdapter DBL = new ezUtilsAdapter();
            DataChecker dtc = new DataChecker();
            DataSet ds = new DataSet();

            try
            {
                ds = DBL.RunSPReturnDataSet("SP_GetPatient", "TR", new SqlParameter("@vchPID", PID));
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    objPatient.PatientID = Convert.ToString(dr["PID"]);
                    objPatient.FName = Convert.ToString(dr["FName"]);
                    objPatient.MName = Convert.ToString(dr["MName"]);
                    objPatient.LName = Convert.ToString(dr["LName"]);
                    objPatient.FatherFName = Convert.ToString(dr["FatherFName"]);
                    objPatient.FatherMName = Convert.ToString(dr["FatherMName"]);
                    objPatient.FatherLName = Convert.ToString(dr["FatherLName"]);
                    objPatient.Address1 = Convert.ToString(dr["Address1"]);
                    objPatient.Address2 = Convert.ToString(dr["Address2"]);
                    objPatient.City = Convert.ToString(dr["City"]);
                    objPatient.State = Convert.ToString(dr["State"]);
                    objPatient.ZipCode = Convert.ToString(dr["ZipCode"]);
                    objPatient.HomePhone = Convert.ToString(dr["HomePhone"]);
                    objPatient.MobilePhone = Convert.ToString(dr["MobilePhone"]);
                    objPatient.OfficePhone = Convert.ToString(dr["OfficePhone"]);
                    DateTime date = Convert.ToDateTime(dr["DOB"]);
                    objPatient.Age = Convert.ToString(DateTime.Now.Year - date.Year);
                    objPatient.Sex = Convert.ToString(dr["Sex"]);
                }
                
                return objPatient;
            }
            catch (SqlException ex)
            {
                string errorMessage = String.Format(
                    "An Error Occurred While Loading the Patient Information to the DB. Message: {0}", ex.Message);
                return objPatient;
            }
            finally
            {
                DBL.Dispose();
                dtc.Dispose();
                ds.Dispose();
            }
        }

        public long Is_PatientCase_Open(string PID, string CRN, PatientRegistration objPatient)
        {
            ezUtilsAdapter DBL = new ezUtilsAdapter();
            DataSet ds = new DataSet();
            long RetVal = -1;

            try
            {
                if (PID != null)
                {
                    ds = DBL.RunSPReturnDataSet("SP_IsPatientCaseOpen", "TR",
                        new SqlParameter("@IDType", "PID"),
                        new SqlParameter("@vchID", PID));
                }
                else if (CRN != null)
                {
                    ds = DBL.RunSPReturnDataSet("SP_IsPatientCaseOpen", "TR",
                    new SqlParameter("@IDType", "CRNumber"),
                    new SqlParameter("@vchID", CRN));
                }
                
                if (ds != null &&
                    ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = ds.Tables[0].Rows[0];
                    if (PID != null)
                    {
                        RetVal = Convert.ToInt64(dr["CRNumber"]);
                    }
                    else if (CRN != null)
                    {
                        RetVal = Convert.ToInt64(dr["PID"]);
                    }
                    if (objPatient != null)
                    {
                        string patientType = Convert.ToString(dr["PatientType"]);
                        if (patientType.Equals("I"))
                        {
                            objPatient.PatientType = "In-Patient";
                        }
                        else if (patientType.Equals("O"))
                        {
                            objPatient.PatientType = "Out-Patient";
                        }
                        objPatient.Consultant = Convert.ToString(dr["Consultant"]);
                    }
                }
                return RetVal;
            }
            catch (SqlException ex)
            {
                return RetVal;
            }
            finally
            {
                DBL.Dispose();
            }
            
        }

        public void Dispose()
        {
            this.Dispose(true);
            System.GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
        }
    }
}